package com.vanysoft.framework.utils.sms;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

//import javax.xml.rpc.ParameterMode;
//import org.apache.axis.client.Call;
//import org.apache.axis.client.Service;
//import org.apache.axis.encoding.XMLType;


public class MessageSender extends Thread {
    public static String strURL = "http://211.99.191.148";
    public static String nameSpace = "http://info.pica.com";
    private static String result = "-1";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String date = sdf.format(new Date());
    private String SHCNN_URI_SEND = "/mms/services/info";
    private String mobileNo;
    private String msg;

    public MessageSender(String mobileNo, String msg) {
        this.mobileNo = mobileNo;
        if (StringUtils.isBlank(msg)) {
            msg = "";
        }
        this.msg = msg;
    }

    public void run() {
        result = sendEx("YCWL-CRM-0100-OWOIBY", "16378387", msg, mobileNo, "000");
        System.out.println("sendSMS =>" + result);
    }

    public String sendSMS() {
        return sendEx("YCWL-CRM-0100-OWOIBY", "16378387", msg, mobileNo, "000");
    }


    private String sendEx(String Regcode, String Pwd, String content, String mobile, String exnum) {
        String result = "";
        byte[] b;
        boolean isSentOk = false;
        int sendtimes = 0;
        while (!isSentOk) {
            try {
                /**
                 Service services = new Service();
                 Call call1 = null;
                 call1 = (Call) services.createCall();
                 call1.setTargetEndpointAddress(new URL(strURL+SHCNN_URI_SEND));
                 call1.setOperationName(new QName(nameSpace,"sendSMS"));
                 call1.addParameter(new QName(nameSpace,"in0"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in1"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in2"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in3"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in4"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in5"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in6"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in7"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in8"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.addParameter(new QName(nameSpace,"in9"), XMLType.XSD_STRING, ParameterMode.IN);
                 call1.setReturnType(XMLType.XSD_STRING);
                 call1.setUseSOAPAction(true);
                 call1.setSOAPActionURI("http://zzwx/example");
                 result = (String) call1.invoke(new Object[] {Regcode, Pwd, mobile, java.net.URLEncoder.encode(content,"gbk"),"", "1", "", "1", "", "4"});
                 b = result.getBytes("8859_1");
                 String name = new String(b, "GBK");
                 if(name.equals("0")){
                 isSentOk=true;
                 }else{
                 sendtimes ++;
                 if (sendtimes>1){
                 isSentOk=true;
                 }else
                 {
                 System.out.println("发送结果：" + name);
                 isSentOk=false;
                 }
                 }**/
            } catch (Exception ex) {
                System.out.println("ex----");
                isSentOk = true;
            }
        }
        if (isSentOk) {
            if (sendtimes > 1)
                return "-2";
            else
                return "0";
        } else {
            return "-1";
        }

    }

//    public static void main(String[] args) {
////		 new MessageSender("15821921595","尊敬的SharpClaw_x，您受邀参加的【会议测试】于06月14日08:00 在 上海市普陀区武宁路955弄举行，届时别忘了准时参加哦！【会议管理】").start();
////		 System.out.println(result);
//		 MessageSender ms = new MessageSender("15821921595", "尊敬的SharpClaw_x，您受邀参加的【会议测试】于06月14日08:00 在 上海市普陀区武宁路955弄举行，届时别忘了准时参加哦！【会议管理】");
//		 String result = ms.sendSMS();
//		 if(StringUtils.equals(result, "0")){
//			 System.out.println("发送成功！");
//		 }
//		 else{
//			 System.out.println("发送失败！");
//		 }
//    }
}